import { Vue, Component} from 'vue-property-decorator'
import { DATATEST, TestItem } from '@/moduleC/api/mock'
import { TestData, isTestItemArray } from '@/moduleC/types/mock'
	import PageContainer from '@/moduleC/components/layout/PageContainer.vue'
interface Shop {
	name: string
	image: string
	averagePrice: string
	personNum: number
	distance: string
	discount: string
}

interface Category {
	title: string
	testData: keyof TestData
	image: string
	shops: Shop[]
}

interface UniSystemInfo extends UniApp.GetSystemInfoResult {
	statusBarHeight: number
}

	@Component({
		components: {
			PageContainer
		}
	})
export default class recommendation extends Vue {
	private statusBarHeight: number = 0
	private navHeight: number = 0
	private categories: Category[] = []

	private baseFn(data: TestItem[]): Shop[] {
		if (!isTestItemArray(data)) {
			console.error('Invalid data format')
			return []
		}
		
		const sportlist: Shop[] = []
		data.forEach(el => {
			const Num = Math.floor(Math.random() * (100 - 10 + 1)) + 10
			const personNum = Math.floor(Math.random() * (100 - 10 + 1)) + 10
			const discount = Math.floor(Math.random() * 100) + 1
			sportlist.push({
				name: el.region,
				image: el.img,
				averagePrice: Num + '+',
				personNum: personNum,
				distance: el.distance,
				discount: discount + '%'
			})
		})
		return sportlist
	}

	created() {
		this.categories = [
			{
				title: '特色美食',
				testData: 'MEISHI',
				image: 'http://sq85owu9k.hb-bkt.clouddn.com/lingpin/static/lottery/img11.png',
				shops: this.baseFn(DATATEST['MEISHI'] as TestItem[]),
			},
			{
				title: '电子产品',
				testData: 'DIANZI',
				image: 'http://sq85owu9k.hb-bkt.clouddn.com/lingpin/static/lottery/img12.png',
				shops: this.baseFn(DATATEST['DIANZI'] as TestItem[]),
			},
			{
				title: '超市便利',
				testData: 'SHOPPING',
				image: 'http://sq85owu9k.hb-bkt.clouddn.com/lingpin/static/lottery/img3.png',
				shops: this.baseFn(DATATEST['SHOPPING'] as TestItem[]),
			},
			{
				title: '运动健身',
				testData: 'SPORT',
				image: 'http://sq85owu9k.hb-bkt.clouddn.com/lingpin/static/lottery/img4.png',
				shops: this.baseFn(DATATEST['SPORT'] as TestItem[]),
			},
			{
				title: '酒店民宿',
				testData: 'HOTEL',
				image: 'http://sq85owu9k.hb-bkt.clouddn.com/lingpin/static/lottery/img5.png',
				shops: this.baseFn(DATATEST['HOTEL'] as TestItem[]),
			}
		]
	}

	onLoad() {
		const systemInfo = uni.getSystemInfoSync() as UniSystemInfo
		this.statusBarHeight = systemInfo.statusBarHeight
		this.navHeight = systemInfo.statusBarHeight + 44
	}

	gotoStore(data: Shop, datatr: keyof TestData, idx: number): void {
		const items = DATATEST[datatr]
		uni.setStorageSync('itemDetail', items[idx])
		uni.navigateTo({
			url: '/moduleA/recommendation/store/index'
		})
	}

	goBack(): void {
		uni.navigateBack({
			delta: 1
		})
	}
}
